package com.zzz.demo.format;

import org.apache.commons.lang.StringUtils;

import javax.swing.*;
import java.awt.*;
import java.awt.event.MouseEvent;
import java.awt.event.MouseListener;

public class SqlFormatJframe extends JFrame {
    private static final long serialVersionUID = 1L;

    JPanel centerPanel;

    JSplitPane jsp;

    JSplitPane jsp2;

    JTextArea sqlJTArea;

    JScrollPane sqlJSPane;

    JTextArea paramJTArea;

    JScrollPane paramJSPane;

    JPanel southPanel;

    JButton sumbitButtom;

    JTextArea resultJTArea;

    JScrollPane resultJSPane;

    public SqlFormatJframe() {
        this.centerPanel = new JPanel();
        this.sqlJTArea = new JTextArea();
        this.sqlJTArea.setLineWrap(true);
        // Add title to sqlJSPane
        this.sqlJSPane = new JScrollPane(this.sqlJTArea);
        this.sqlJSPane.setBorder(BorderFactory.createTitledBorder("请填写SQL"));

        this.paramJTArea = new JTextArea();
        this.paramJTArea.setLineWrap(true);
        // Add title to paramJSPane
        this.paramJSPane = new JScrollPane(this.paramJTArea);
        this.paramJSPane.setBorder(BorderFactory.createTitledBorder("请填写参数"));

        //上下分区
        this.jsp = new JSplitPane(JSplitPane.VERTICAL_SPLIT, this.sqlJSPane, this.paramJSPane);
        this.jsp.setDividerLocation(269);
        this.jsp.setDividerSize(10);
        this.resultJTArea = new JTextArea();
        this.resultJTArea.setLineWrap(true);
        this.resultJSPane = new JScrollPane(this.resultJTArea);

        //左右分区
        this.jsp2 = new JSplitPane(JSplitPane.HORIZONTAL_SPLIT, this.jsp, this.resultJSPane);
        this.jsp2.setDividerLocation(608);
        this.jsp2.setDividerSize(10);
        this.centerPanel.setLayout(new BorderLayout());
        this.centerPanel.add(this.jsp2);

        this.southPanel = new JPanel();
        this.southPanel.setLayout(new BorderLayout());
        this.sumbitButtom = new JButton("生成最终SQL");
        this.southPanel.add(this.sumbitButtom);

        this.sumbitButtom.addMouseListener(new MouseListener() {
            public void mouseReleased(MouseEvent e) {
            }

            public void mousePressed(MouseEvent e) {
            }

            public void mouseExited(MouseEvent e) {
            }

            public void mouseEntered(MouseEvent e) {
            }

            public void mouseClicked(MouseEvent e) {
                String sql = sqlJTArea.getText();
                String param = paramJTArea.getText();
                String result = sql;
                if (StringUtils.isNotBlank(sql) && StringUtils.isNotBlank(param)) {
                    result = SqlFormatTool.genSQL(sql, param);
                } else if (StringUtils.isNotBlank(sql)) {
                    result = SqlFormatTool.format(sql);
                }
                SqlFormatJframe.this.resultJTArea.setText(result);
            }
        });

        add(this.centerPanel, BorderLayout.CENTER);
        add(this.southPanel, BorderLayout.SOUTH);
        setTitle("SQL FORMAT");
        setSize(1200, 600);
        setLocation(10, 10);
        setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
        setVisible(true);
    }
}
